<template>
  <Dialog :title="dialogTitle" v-model="dialogVisible">
    <el-form
      ref="formRef"
      :model="formData"
      :rules="formRules"
      label-width="100px"
      v-loading="formLoading"
    >
      <el-form-item label="计划编号" prop="pplanCode">
        <el-input v-model="formData.pplanCode" placeholder="请输入计划编号" />
      </el-form-item>
      <el-form-item label="航班号" prop="flightNo">
        <el-input v-model="formData.flightNo" placeholder="请输入航班号" />
      </el-form-item>
      <el-form-item label="班期" prop="schPat">
        <el-input v-model="formData.schPat" placeholder="请输入班期" />
      </el-form-item>
      <el-form-item label="起飞地点" prop="depLoc">
        <el-input v-model="formData.depLoc" placeholder="请输入起飞地点" />
      </el-form-item>
      <!--<el-form-item label="起飞站点三字代码" prop="depIata">-->
      <!--  <el-input v-model="formData.depIata" placeholder="请输入起飞站点三字代码" />-->
      <!--</el-form-item>-->
      <el-form-item label="起飞时间" prop="depTime">
        <el-time-picker
          v-model="formData.depTime"
          format="HH:mm"
          value-format="HH:mm"
          placeholder="选择起飞时间"
          clearable
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="降落地点" prop="arrLoc">
        <el-input v-model="formData.arrLoc" placeholder="请输入降落地点" />
      </el-form-item>
      <!--<el-form-item label="降落站点三字代码" prop="arrIata">-->
      <!--  <el-input v-model="formData.arrIata" placeholder="请输入降落站点三字代码" />-->
      <!--</el-form-item>-->
      <el-form-item label="降落时间" prop="arrTime">
        <el-time-picker
          v-model="formData.arrTime"
          format="HH:mm"
          value-format="HH:mm"
          placeholder="选择起飞时间"
          clearable
          class="!w-240px"
        />
      </el-form-item>
      <!--<el-form-item label="航班性质" prop="flightType">-->
      <!--  <el-select v-model="formData.flightType" placeholder="请选择航班性质">-->
      <!--    <el-option label="请选择字典生成" value="" />-->
      <!--  </el-select>-->
      <!--</el-form-item>-->
      <!--<el-form-item label="商务舱餐谱" prop="bussClassMeal">-->
      <!--  <el-input v-model="formData.bussClassMeal" placeholder="请输入商务舱餐谱" />-->
      <!--</el-form-item>-->
      <!--<el-form-item label="商务舱特殊餐餐谱" prop="bussClassSpcMeal">-->
      <!--  <el-input v-model="formData.bussClassSpcMeal" placeholder="请输入商务舱特殊餐餐谱" />-->
      <!--</el-form-item>-->
      <!--<el-form-item label="高级经济舱餐谱" prop="premEcoClassMeal">-->
      <!--  <el-input v-model="formData.premEcoClassMeal" placeholder="请输入高级经济舱餐谱" />-->
      <!--</el-form-item>-->
      <!--<el-form-item label="高级经济舱特殊餐餐谱" prop="premEcoClassSpcMeal">-->
      <!--  <el-input v-model="formData.premEcoClassSpcMeal" placeholder="请输入高级经济舱特殊餐餐谱" />-->
      <!--</el-form-item>-->
      <!--<el-form-item label="经济舱餐谱" prop="ecoClassMeal">-->
      <!--  <el-input v-model="formData.ecoClassMeal" placeholder="请输入经济舱餐谱" />-->
      <!--</el-form-item>-->
      <!--<el-form-item label="经济舱机组餐谱" prop="ecoClassSpcMeal">-->
      <!--  <el-input v-model="formData.ecoClassSpcMeal" placeholder="请输入经济舱机组餐谱" />-->
      <!--</el-form-item>-->
      <!--<el-form-item label="机组餐谱" prop="crewMeal">-->
      <!--  <el-input v-model="formData.crewMeal" placeholder="请输入机组餐谱" />-->
      <!--</el-form-item>-->
      <!--<el-form-item label="所属公司" prop="airlineComp">-->
      <!--  <el-input v-model="formData.airlineComp" placeholder="请输入所属公司" />-->
      <!--</el-form-item>-->
      <!--<el-form-item label="所属公司三字代码" prop="airlineIata">-->
      <!--  <el-input v-model="formData.airlineIata" placeholder="请输入所属公司三字代码" />-->
      <!--</el-form-item>-->
      <!--<el-form-item label="航线类别" prop="airlineType">-->
      <!--  <el-select v-model="formData.airlineType" placeholder="请选择航线类别">-->
      <!--    <el-option label="请选择字典生成" value="" />-->
      <!--  </el-select>-->
      <!--</el-form-item>-->
      <!--<el-form-item label="商务舱人数量" prop="bussClassQty">-->
      <!--  <el-input v-model="formData.bussClassQty" placeholder="请输入商务舱人数量" />-->
      <!--</el-form-item>-->
      <!--<el-form-item label="高级经济舱人数量" prop="premEcoClassQty">-->
      <!--  <el-input v-model="formData.premEcoClassQty" placeholder="请输入高级经济舱人数量" />-->
      <!--</el-form-item>-->
      <!--<el-form-item label="经济舱人数量" prop="ecoClassQty">-->
      <!--  <el-input v-model="formData.ecoClassQty" placeholder="请输入经济舱人数量" />-->
      <!--</el-form-item>-->
      <!--<el-form-item label="机组人数量" prop="crewQty">-->
      <!--  <el-input v-model="formData.crewQty" placeholder="请输入机组人数量" />-->
      <!--</el-form-item>-->
      <el-form-item label="商务舱特殊人数量" prop="bussClassSpcQty">
        <el-input v-model="formData.bussClassSpcQty" placeholder="请输入商务舱特殊人数量" />
      </el-form-item>
      <el-form-item label="高级经济舱特殊人数量" prop="premEcoClassSpcQty">
        <el-input v-model="formData.premEcoClassSpcQty" placeholder="请输入高级经济舱特殊人数量" />
      </el-form-item>
      <el-form-item label="经济舱特殊人数量" prop="ecoClassSpcQty">
        <el-input v-model="formData.ecoClassSpcQty" placeholder="请输入经济舱特殊人数量" />
      </el-form-item>
      <el-form-item label="机组特殊人数量" prop="crewSpcQty">
        <el-input v-model="formData.crewSpcQty" placeholder="请输入机组特殊人数量" />
      </el-form-item>
      <el-form-item label="商务舱餐数量" prop="bussClassMealQty">
        <el-input v-model="formData.bussClassMealQty" placeholder="请输入商务舱餐数量" />
      </el-form-item>
      <el-form-item label="高级经济舱餐数量" prop="premEcoClassMealQty">
        <el-input v-model="formData.premEcoClassMealQty" placeholder="请输入高级经济舱餐数量" />
      </el-form-item>
      <el-form-item label="经济舱餐数量" prop="ecoClassMealQty">
        <el-input v-model="formData.ecoClassMealQty" placeholder="请输入经济舱餐数量" />
      </el-form-item>
      <el-form-item label="机组餐数量" prop="crewMealQty">
        <el-input v-model="formData.crewMealQty" placeholder="请输入机组餐数量" />
      </el-form-item>
      <el-form-item label="商务舱特殊餐数量" prop="bussClassSpcMealQty">
        <el-input v-model="formData.bussClassSpcMealQty" placeholder="请输入商务舱特殊餐数量" />
      </el-form-item>
      <el-form-item label="高级经济舱特殊餐数量" prop="premEcoClassSpcMealQty">
        <el-input v-model="formData.premEcoClassSpcMealQty" placeholder="请输入高级经济舱特殊餐数量" />
      </el-form-item>
      <el-form-item label="经济舱特殊餐数量" prop="ecoClassSpcMealQty">
        <el-input v-model="formData.ecoClassSpcMealQty" placeholder="请输入经济舱特殊餐数量" />
      </el-form-item>
      <el-form-item label="机组特殊餐数量" prop="crewSpcMealQty">
        <el-input v-model="formData.crewSpcMealQty" placeholder="请输入机组特殊餐数量" />
      </el-form-item>
      <el-form-item label="餐车数量" prop="dinerCars">
        <el-input v-model="formData.dinerCars" placeholder="请输入餐车数量" />
      </el-form-item>
      <el-form-item label="烤炉数量" prop="ovens">
        <el-input v-model="formData.ovens" placeholder="请输入烤炉数量" />
      </el-form-item>
      <el-form-item label="备注" prop="remarks">
        <el-input v-model="formData.remarks" placeholder="请输入备注" />
      </el-form-item>
      <el-form-item label="特殊说明" prop="specialRemarks">
        <el-input v-model="formData.specialRemarks" placeholder="请输入特殊说明" />
      </el-form-item>
      <!--<el-form-item label="记录日期" prop="recordDate">-->
      <!--  <el-date-picker-->
      <!--    v-model="formData.recordDate"-->
      <!--    type="date"-->
      <!--    value-format="x"-->
      <!--    placeholder="选择记录日期"-->
      <!--  />-->
      <!--</el-form-item>-->
      <el-form-item label="标记状态" prop="markStatus">
        <el-select v-model="formData.markStatus" placeholder="请选择标记状态">
          <el-option
            v-for="item in getIntDictOptions(DICT_TYPE.PPLAN_PRODUCE_MARK_STATUS)"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="生产状态" prop="status">
        <el-select v-model="formData.status" placeholder="请选择生产状态">
          <el-option
            v-for="item in getIntDictOptions(DICT_TYPE.PPLAN_PRODUCE_STATUS)"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
      </el-form-item>
    </el-form>
    <template #footer>
      <!--<el-button @click="submitForm" type="primary" :disabled="formLoading">确 定</el-button>-->
      <el-button @click="dialogVisible = false">取 消</el-button>
    </template>
  </Dialog>
</template>
<script setup lang="ts">
import { QuarterProduceRecordApi, QuarterProduceRecordVO } from '@/api/pplan/quarterproducerecord'
import { DICT_TYPE, getIntDictOptions, getStrDictOptions } from '@/utils/dict'

/** 生产计划-生产季度计划记录 表单 */
defineOptions({ name: 'QuarterProduceRecordForm' })

const { t } = useI18n() // 国际化
const message = useMessage() // 消息弹窗

const dialogVisible = ref(false) // 弹窗的是否展示
const dialogTitle = ref('') // 弹窗的标题
const formLoading = ref(false) // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
const formType = ref('') // 表单的类型：create - 新增；update - 修改
const formData = ref({
  id: undefined,
  pplanCode: undefined,
  flightNo: undefined,
  schPat: undefined,
  depLoc: undefined,
  depIata: undefined,
  depTime: undefined,
  arrLoc: undefined,
  arrIata: undefined,
  arrTime: undefined,
  flightType: undefined,
  bussClassMeal: undefined,
  bussClassSpcMeal: undefined,
  premEcoClassMeal: undefined,
  premEcoClassSpcMeal: undefined,
  ecoClassMeal: undefined,
  ecoClassSpcMeal: undefined,
  crewMeal: undefined,
  airlineComp: undefined,
  airlineIata: undefined,
  airlineType: undefined,
  bussClassQty: undefined,
  premEcoClassQty: undefined,
  ecoClassQty: undefined,
  crewQty: undefined,
  bussClassSpcQty: undefined,
  premEcoClassSpcQty: undefined,
  ecoClassSpcQty: undefined,
  crewSpcQty: undefined,
  bussClassMealQty: undefined,
  premEcoClassMealQty: undefined,
  ecoClassMealQty: undefined,
  crewMealQty: undefined,
  bussClassSpcMealQty: undefined,
  premEcoClassSpcMealQty: undefined,
  ecoClassSpcMealQty: undefined,
  crewSpcMealQty: undefined,
  dinerCars: undefined,
  ovens: undefined,
  remarks: undefined,
  specialRemarks: undefined,
  recordDate: undefined,
  markStatus: undefined,
  status: undefined
})
const formRules = reactive({
  pplanCode: [{ required: true, message: '计划编号不能为空', trigger: 'blur' }],
  flightNo: [{ required: true, message: '航班号不能为空', trigger: 'blur' }],
  depTime: [{ required: true, message: '起飞时间不能为空', trigger: 'blur' }],
  markStatus: [{ required: true, message: '标记状态:1未标记;不能为空', trigger: 'change' }],
  status: [{ required: true, message: '生产状态，默认1未开始2进行中3已结束不能为空', trigger: 'blur' }]
})
const formRef = ref() // 表单 Ref

/** 打开弹窗 */
const open = async (type: string, id?: number) => {
  dialogVisible.value = true
  dialogTitle.value = t('action.' + type)
  formType.value = type
  resetForm()
  // 修改时，设置数据
  if (id) {
    formLoading.value = true
    try {
      formData.value = await QuarterProduceRecordApi.getQuarterProduceRecord(id)
    } finally {
      formLoading.value = false
    }
  }
}
defineExpose({ open }) // 提供 open 方法，用于打开弹窗

/** 提交表单 */
const emit = defineEmits(['success']) // 定义 success 事件，用于操作成功后的回调
const submitForm = async () => {
  // 校验表单
  await formRef.value.validate()
  // 提交请求
  formLoading.value = true
  try {
    const data = formData.value as unknown as QuarterProduceRecordVO
    if (formType.value === 'create') {
      await QuarterProduceRecordApi.createQuarterProduceRecord(data)
      message.success(t('common.createSuccess'))
    } else {
      await QuarterProduceRecordApi.updateQuarterProduceRecord(data)
      message.success(t('common.updateSuccess'))
    }
    dialogVisible.value = false
    // 发送操作成功的事件
    emit('success')
  } finally {
    formLoading.value = false
  }
}

/** 重置表单 */
const resetForm = () => {
  formData.value = {
    id: undefined,
    pplanCode: undefined,
    flightNo: undefined,
    schPat: undefined,
    depLoc: undefined,
    depIata: undefined,
    depTime: undefined,
    arrLoc: undefined,
    arrIata: undefined,
    arrTime: undefined,
    flightType: undefined,
    bussClassMeal: undefined,
    bussClassSpcMeal: undefined,
    premEcoClassMeal: undefined,
    premEcoClassSpcMeal: undefined,
    ecoClassMeal: undefined,
    ecoClassSpcMeal: undefined,
    crewMeal: undefined,
    airlineComp: undefined,
    airlineIata: undefined,
    airlineType: undefined,
    bussClassQty: undefined,
    premEcoClassQty: undefined,
    ecoClassQty: undefined,
    crewQty: undefined,
    bussClassSpcQty: undefined,
    premEcoClassSpcQty: undefined,
    ecoClassSpcQty: undefined,
    crewSpcQty: undefined,
    bussClassMealQty: undefined,
    premEcoClassMealQty: undefined,
    ecoClassMealQty: undefined,
    crewMealQty: undefined,
    bussClassSpcMealQty: undefined,
    premEcoClassSpcMealQty: undefined,
    ecoClassSpcMealQty: undefined,
    crewSpcMealQty: undefined,
    dinerCars: undefined,
    ovens: undefined,
    remarks: undefined,
    specialRemarks: undefined,
    recordDate: undefined,
    markStatus: undefined,
    status: undefined
  }
  formRef.value?.resetFields()
}
</script>
